package com.wangmeng.book.updater;

import com.wangmeng.book.dao.BaseDaoSupport;
import com.wangmeng.book.entity.*;
import com.wangmeng.book.utils.ConstValueUtils;
import com.wangmeng.book.utils.SpringContextManager;
import com.wangmeng.book.utils.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.log4j.Logger;
import org.cyberneko.html.parsers.DOMParser;
import org.hibernate.engine.jdbc.BlobProxy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.w3c.dom.*;
import org.xml.sax.InputSource;

import java.io.ByteArrayOutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * User: 王猛
 * Date: 13-9-12
 */
public class BookFromSina implements IBookUpdater {
    private static final Logger logger = Logger.getLogger(BookFromSina.class);
    private static final String CHARSET_HTML = "GBK";
    private static final int PAGE_SIZE = 80;
    private static final String URL_INDEX = "http://vip.book.sina.com.cn/book_lib.php?lib=001&p=#{page}&r=" + PAGE_SIZE + "&order=rwc&sta=&pub=2&dpc=1";
    private static final String HREF_FIX = "http://vip.book.sina.com.cn";

    public void updateList() {
        Document doc = getDocument(URL_INDEX.replace("#{page}", "1"));
        if (doc != null) {
            NodeList divList = doc.getElementsByTagName("DIV");
            BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
            JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
            String checkSql = " and name = ? and author = ? ";
            String checkTagSql = "select oid from book_document_tag where tag_name = ? ";
            Integer totalNum = 0;
            for (int i = 0; i < divList.getLength(); i++) {
                Node divNode = divList.item(i);
                if (divNode.hasAttributes()) {
                    NamedNodeMap attrMap = divNode.getAttributes();
                    Node clsAttrNode = attrMap.getNamedItem("class");
                    if (clsAttrNode != null) {
                        if (clsAttrNode.getNodeValue().contains("blk_num")) {
                            String totalStr = divNode.getChildNodes().item(1).getTextContent().trim();
                            if (!StringUtils.isTrimEmpty(totalStr)) {
                                totalNum = Integer.valueOf(totalStr);
                            }
                        } else if (clsAttrNode.getNodeValue().contains("blk_book")) {
                            NodeList iList = ((Element) divNode).getElementsByTagName("I");
                            if (iList.getLength() == 0) {
                                //VIP的不下载
                                NodeList aList = ((Element) divNode).getElementsByTagName("A");
                                Node hrefNode = aList.item(0);
                                String hrefStr = hrefNode.getAttributes().getNamedItem("href").getNodeValue();
                                String iconHref = ((Element) hrefNode).getElementsByTagName("IMG").item(0).getAttributes().getNamedItem("src").getNodeValue();
                                String title = aList.item(1).getTextContent().trim();
                                NodeList ulList = ((Element) divNode).getElementsByTagName("UL");
                                NodeList liList = ((Element) ulList.item(0)).getElementsByTagName("LI");
                                String author = liList.item(0).getTextContent().trim();//作者：朝天椒
                                String[] authorInfos = author.split("：");
                                if (authorInfos.length > 1) {
                                    author = authorInfos[1].trim();
                                }
                                //
                                List<BookDoc> bookList = daoSupport.listEntityByCondition(BookDoc.class, checkSql, new Object[]{title, author});
                                if (bookList.size() == 0) {
                                    try {
                                        String status = liList.item(1).getTextContent().trim();//状态：连载
                                        liList = ((Element) ulList.item(1)).getElementsByTagName("LI");
                                        String partition = liList.item(0).getTextContent().trim();//分类：军事小说
                                        String publisher = "";
                                        if (liList.getLength() > 1) {
                                            publisher = liList.item(1).getTextContent().trim();//出版：重庆出版社
                                            String[] pInfo = publisher.split("：");
                                            if (pInfo.length > 1) {
                                                publisher = pInfo[1].trim();
                                            }
                                        }
                                        liList = ((Element) ulList.item(2)).getElementsByTagName("LI");
                                        String vcStr = liList.getLength() > 0 ? liList.item(0).getTextContent().trim() : "";
                                        NodeList pList = ((Element) divNode).getElementsByTagName("P");
                                        String brief = pList.item(0).getTextContent().trim();
                                        BookDoc bd = new BookDoc();
                                        bd.setName(title);
                                        bd.setAuthor(author);
                                        bd.setBookBrief(brief);
                                        bd.setPublisher(publisher);
                                        bd.setSourceType(ConstValueUtils.SOURCE_TYPE_SN);
                                        bd.setSourceId(hrefStr);
                                        bd.setWordNum(0);
                                        bd.setType(ConstValueUtils.BOOK_TYPE_COMMON);
                                        String[] statusInfo = status.split("：");
                                        if (statusInfo.length > 1) {
                                            status = statusInfo[1].trim();
                                        }
                                        bd.setStatus(StringUtils.isTrimEmpty(status) ? "" : (status.equals("连载") ? ConstValueUtils.STATUS_CONTINUE : ConstValueUtils.STATUS_CONTINUE));
                                        String[] pInfos = partition.split("：");
                                        bd.setCateName(pInfos.length > 1 ? pInfos[1].trim() : partition);
                                        //处理图标信息
                                        bd.setHasIcon(false);
                                        if (!StringUtils.isTrimEmpty(iconHref)) {
                                            byte[] imgData = downHttpData(iconHref);
                                            if (imgData != null && imgData.length > 0) {
                                                bd.setHasIcon(true);
                                                bd.setIcon(BlobProxy.generateProxy(imgData));
                                            }
                                        }
                                        bd.setApproveStatus(ConstValueUtils.APPROVE_INIT);
                                        bd.setFree(1);
                                        bd.setHref(hrefStr);
                                        if (vcStr != null) {
                                            String[] vcStrs = vcStr.split("：");
                                            if (vcStrs.length == 2) {
                                                bd.setViewCount(Integer.valueOf(vcStrs[1].trim().replace(",", "")));
                                            }
                                        }
                                        bd.setUpdateTime(new Date());
                                        daoSupport.saveEntity(bd);
                                        NodeList keyAList = ((Element) pList.item(1)).getElementsByTagName("A");
                                        if (keyAList.getLength() > 0) {
                                            for (int t = 0; t < keyAList.getLength(); t++) {
                                                String keyStr = keyAList.item(t).getTextContent().trim();
                                                int tagId;
                                                List<Map<String, Object>> tagDbList = jt.queryForList(checkTagSql, new Object[]{keyStr});
                                                if (tagDbList.size() > 0) {
                                                    tagId = ((Number) tagDbList.get(0).get("oid")).intValue();
                                                } else {
                                                    BookDocTag bt = new BookDocTag();
                                                    bt.setName(keyStr);
                                                    daoSupport.saveEntity(bt);
                                                    tagId = bt.getId();
                                                }
                                                BookDocTagRelation tr = new BookDocTagRelation();
                                                tr.setBookId(bd.getId());
                                                tr.setTagId(tagId);
                                                daoSupport.saveEntity(tr);
                                            }
                                        }
                                        //更新内容
//                                    updateList(bd);
                                    } catch (Exception ex) {
                                        logger.error(ex);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (totalNum > PAGE_SIZE) {
                for (int page = 2; (page - 1) * PAGE_SIZE < totalNum; page++) {
                    doc = getDocument(URL_INDEX.replace("#{page}", String.valueOf(page)));
                    try {
                        if (doc != null) {
                            divList = doc.getElementsByTagName("DIV");
                            for (int i = 0; i < divList.getLength(); i++) {
                                Node divNode = divList.item(i);
                                NodeList iList = ((Element) divNode).getElementsByTagName("I");
                                if (iList.getLength() == 0) {
                                    if (divNode.hasAttributes()) {
                                        NamedNodeMap attrMap = divNode.getAttributes();
                                        Node clsAttrNode = attrMap.getNamedItem("class");
                                        if (clsAttrNode != null) {
                                            if (clsAttrNode.getNodeValue().contains("blk_book")) {
                                                iList = ((Element) divNode).getElementsByTagName("I");
                                                if (iList.getLength() == 0) {
                                                    try {
                                                        NodeList aList = ((Element) divNode).getElementsByTagName("A");
                                                        Node hrefNode = aList.item(0);
                                                        String hrefStr = hrefNode.getAttributes().getNamedItem("href").getNodeValue();
                                                        String iconHref = ((Element) hrefNode).getElementsByTagName("IMG").item(0).getAttributes().getNamedItem("src").getNodeValue();
                                                        String title = aList.item(1).getTextContent().trim();
                                                        NodeList ulList = ((Element) divNode).getElementsByTagName("UL");
                                                        NodeList liList = ((Element) ulList.item(0)).getElementsByTagName("LI");
                                                        String author = liList.item(0).getTextContent().trim();//作者：朝天椒
                                                        String[] authorInfos = author.split("：");
                                                        if (authorInfos.length > 1) {
                                                            author = authorInfos[1].trim();
                                                        }
                                                        //
                                                        List<BookDoc> bookList = daoSupport.listEntityByCondition(BookDoc.class, checkSql, new Object[]{title, author});
                                                        if (bookList.size() == 0) {
                                                            String status = liList.item(1).getTextContent().trim();//状态：连载
                                                            liList = ((Element) ulList.item(1)).getElementsByTagName("LI");
                                                            String partition = liList.item(0).getTextContent().trim();//分类：军事小说
                                                            String publisher = "";
                                                            if (liList.getLength() > 1) {
                                                                publisher = liList.item(1).getTextContent().trim();//出版：重庆出版社
                                                                String[] pInfo = publisher.split("：");
                                                                if (pInfo.length > 1) {
                                                                    publisher = pInfo[1].trim();
                                                                }
                                                            }
                                                            liList = ((Element) ulList.item(2)).getElementsByTagName("LI");
                                                            String vcStr = liList.getLength() > 0 ? liList.item(0).getTextContent().trim() : "";
                                                            NodeList pList = ((Element) divNode).getElementsByTagName("P");
                                                            String brief = pList.item(0).getTextContent().trim();
                                                            BookDoc bd = new BookDoc();
                                                            bd.setName(title);
                                                            bd.setAuthor(author);
                                                            bd.setBookBrief(brief);
                                                            bd.setPublisher(publisher);
                                                            bd.setSourceType(ConstValueUtils.SOURCE_TYPE_SN);
                                                            bd.setSourceId(hrefStr);
                                                            bd.setWordNum(0);
                                                            bd.setType(ConstValueUtils.BOOK_TYPE_COMMON);
                                                            String[] statusInfo = status.split("：");
                                                            if (statusInfo.length > 1) {
                                                                status = statusInfo[1].trim();
                                                            }
                                                            bd.setStatus(StringUtils.isTrimEmpty(status) ? "" : (status.equals("连载") ? ConstValueUtils.STATUS_CONTINUE : ConstValueUtils.STATUS_CONTINUE));
                                                            String[] pInfos = partition.split("：");
                                                            bd.setCateName(pInfos.length > 1 ? pInfos[1].trim() : partition);
                                                            //处理图标信息
                                                            bd.setHasIcon(false);
                                                            if (!StringUtils.isTrimEmpty(iconHref)) {
                                                                byte[] imgData = downHttpData(iconHref);
                                                                if (imgData != null && imgData.length > 0) {
                                                                    bd.setHasIcon(true);
                                                                    bd.setIcon(BlobProxy.generateProxy(imgData));
                                                                }
                                                            }
                                                            bd.setApproveStatus(ConstValueUtils.APPROVE_INIT);
                                                            bd.setFree(1);
                                                            bd.setHref(hrefStr);
                                                            if (vcStr != null) {
                                                                String[] vcStrs = vcStr.split("：");
                                                                if (vcStrs.length == 2) {
                                                                    bd.setViewCount(Integer.valueOf(vcStrs[1].trim().replace(",", "")));
                                                                }
                                                            }
                                                            bd.setUpdateTime(new Date());
                                                            daoSupport.saveEntity(bd);
                                                            NodeList keyAList = ((Element) pList.item(1)).getElementsByTagName("A");
                                                            if (keyAList.getLength() > 0) {
                                                                for (int t = 0; t < keyAList.getLength(); t++) {
                                                                    String keyStr = keyAList.item(t).getTextContent().trim();
                                                                    int tagId;
                                                                    List<Map<String, Object>> tagDbList = jt.queryForList(checkTagSql, new Object[]{keyStr});
                                                                    if (tagDbList.size() > 0) {
                                                                        tagId = ((Number) tagDbList.get(0).get("oid")).intValue();
                                                                    } else {
                                                                        BookDocTag bt = new BookDocTag();
                                                                        bt.setName(keyStr);
                                                                        daoSupport.saveEntity(bt);
                                                                        tagId = bt.getId();
                                                                    }
                                                                    BookDocTagRelation tr = new BookDocTagRelation();
                                                                    tr.setBookId(bd.getId());
                                                                    tr.setTagId(tagId);
                                                                    daoSupport.saveEntity(tr);
                                                                }
                                                            }
                                                        }
                                                    } catch (Exception ex) {
                                                        logger.error(ex);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } catch (Exception ex) {
                        logger.error("更新第" + page + "页异常", ex);
                    }
                }
            }
        }
    }

    public int loadContent(BookDoc bd) {
        try {
            int wordNum = bd.getWordNum();
            Document doc = getDocument(bd.getSourceId());
            if (doc != null) {
                NodeList divList = doc.getElementsByTagName("DIV");
                BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
                JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
                List<BookDocSection> sectionList = daoSupport.listEntityByCondition(BookDocSection.class, " and bookId = ? order by order asc", new Object[]{bd.getId()});
                String maxPartSql = "select IFNULL(max(part_order),0) mo from book_document_part where section_id in ( select oid from book_document_section where book_id = ? )";
                int maxPartNum = jt.queryForInt(maxPartSql, new Object[]{bd.getId()});
                int sectionNum = 0;
                int pageNum = 0;
                bookLabel:
                for (int d = 0; d < divList.getLength(); d++) {
                    //chapter_list
                    Node divNode = divList.item(d);
                    if (divNode.hasAttributes()) {
                        NamedNodeMap attrMap = divNode.getAttributes();
                        Node clsAttrNode = attrMap.getNamedItem("class");
                        if (clsAttrNode != null) {
                            if (clsAttrNode.getNodeValue().contains("headerTitle")) {
                                NodeList spList = ((Element) divNode).getElementsByTagName("SPAN");
                                if (spList.getLength() > 0) {
                                    String clsName = spList.item(0).getAttributes().getNamedItem("class").getNodeValue().trim();
                                    if (clsName.contains("over")) {
                                        bd.setStatus(ConstValueUtils.STATUS_FINISHED);
                                    } else if (clsName.contains("going")) {
                                        bd.setStatus(ConstValueUtils.STATUS_CONTINUE);
                                    } else {
                                        bd.setStatus(ConstValueUtils.STATUS_SELECTED);
                                    }
                                    daoSupport.saveEntity(bd);
                                }
                            } else if (clsAttrNode.getNodeValue().contains("chapter_list")) {
                                //列表
                                sectionNum++;
                                String sectionName = "第" + sectionNum + "章";
                                NodeList hList = ((Element) divNode).getElementsByTagName("H3");
                                if (hList.getLength() > 0) {
                                    sectionName = hList.item(0).getTextContent().trim();
                                }
                                int sectionId = 0;
                                if (sectionNum <= sectionList.size()) {
                                    BookDocSection bds = sectionList.get(sectionNum - 1);
                                    if (!bds.getName().equals(sectionName)) {
                                        bds.setName(sectionName);
                                        daoSupport.saveEntity(bds);
                                        sectionId = bds.getId();
                                    }
                                } else {
                                    BookDocSection bds = new BookDocSection();
                                    bds.setName(sectionName);
                                    bds.setOrder(sectionNum);
                                    bds.setBookId(bd.getId());
                                    daoSupport.saveEntity(bds);
                                    sectionId = bds.getId();
                                }
                                NodeList pageList = ((Element) divNode).getElementsByTagName("LI");
                                for (int l = 0; l < pageList.getLength(); l++) {
                                    Node liNode = pageList.item(l);
                                    if (!liNode.hasAttributes() || !liNode.getAttributes().getNamedItem("class").getNodeValue().contains("dashedline")) {
                                        NodeList aList = ((Element) liNode).getElementsByTagName("A");
                                        if (aList.getLength() > 0) {
                                            pageNum++;
                                            if (pageNum > maxPartNum) {
                                                Node hrefNode = aList.item(0);
                                                String pageTitle = hrefNode.getTextContent().trim();
                                                String pageHref = hrefNode.getAttributes().getNamedItem("href").getNodeValue().trim();
                                                //更新页内容
                                                Document pageDoc = getDocument(HREF_FIX + pageHref);
                                                if (pageDoc == null) {
                                                    try {
                                                        Thread.sleep(5000);
                                                    } catch (InterruptedException e) {
                                                        e.printStackTrace();
                                                    }
                                                    pageDoc = getDocument(HREF_FIX + pageHref);
                                                    if (pageDoc == null) {
                                                        continue;
                                                    }
                                                }
                                                Element txtEmt = pageDoc.getElementById("contTxt");
                                                if (txtEmt != null) {
                                                    NodeList pList = txtEmt.getElementsByTagName("P");
                                                    StringBuffer contextBuffer = new StringBuffer();
                                                    for (int p = 0; p < pList.getLength(); p++) {
                                                        String pText = pList.item(p).getTextContent().trim();
                                                        if(pText.startsWith("未完待续")||pText.startsWith("新浪原创")){
                                                            break;
                                                        }
                                                        wordNum += pText.length();
                                                        contextBuffer.append("<p>").append(pText).append("</p>");
                                                    }
                                                    BookDocPart bdp = new BookDocPart();
                                                    bdp.setOrder(pageNum);
                                                    bdp.setTitle(pageTitle);
                                                    bdp.setContent(contextBuffer.toString());
                                                    bdp.setStoreType(ConstValueUtils.STORE_TYPE_DB);
                                                    bdp.setSectionId(sectionId);
                                                    bdp.setUpdateTime(new Date());
                                                    daoSupport.saveEntity(bdp);
                                                } else {
                                                    break bookLabel;
                                                }
                                                try {
                                                    Thread.sleep(1000);
                                                } catch (InterruptedException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                bd.setWordNum(wordNum);
                daoSupport.saveEntity(bd);
            }
        } catch (Exception ex) {
            return ConstValueUtils.CODE_EXCEPTION;
        }
        return ConstValueUtils.CODE_SUCCESS;
    }

    public int updateContent(BookDoc bd) {
        try {
            int wordNum = bd.getWordNum();
            Document doc = getDocument(bd.getSourceId());
            if (doc != null) {
                NodeList divList = doc.getElementsByTagName("DIV");
                BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
                JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
                List<BookDocSection> sectionList = daoSupport.listEntityByCondition(BookDocSection.class, " and bookId = ? order by order asc", new Object[]{bd.getId()});
                String maxPartSql = "select IFNULL(max(part_order),0) mo from book_document_part where section_id in ( select oid from book_document_section where book_id = ? )";
                int maxPartNum = jt.queryForInt(maxPartSql, new Object[]{bd.getId()});
                int seq = maxPartNum;
                int sectionNum = 0;
                int pageNum = 0;
                bookLabel:
                for (int d = 0; d < divList.getLength(); d++) {
                    //chapter_list
                    Node divNode = divList.item(d);
                    if (divNode.hasAttributes()) {
                        NamedNodeMap attrMap = divNode.getAttributes();
                        Node clsAttrNode = attrMap.getNamedItem("class");
                        if (clsAttrNode != null) {
                            if (clsAttrNode.getNodeValue().contains("headerTitle")) {
                                NodeList spList = ((Element) divNode).getElementsByTagName("SPAN");
                                if (spList.getLength() > 0) {
                                    String clsName = spList.item(0).getAttributes().getNamedItem("class").getNodeValue().trim();
                                    if (clsName.contains("over")) {
                                        bd.setStatus(ConstValueUtils.STATUS_FINISHED);
                                        daoSupport.saveEntity(bd);
                                    }
                                }
                            } else if (clsAttrNode.getNodeValue().contains("chapter_list")) {
                                //列表
                                sectionNum++;
                                String sectionName = "第" + sectionNum + "章";
                                NodeList hList = ((Element) divNode).getElementsByTagName("H3");
                                if (hList.getLength() > 0) {
                                    sectionName = hList.item(0).getTextContent().trim();
                                }
                                int sectionId = 0;
                                if (sectionNum <= sectionList.size()) {
                                    BookDocSection bds = sectionList.get(sectionNum - 1);
                                    if (!bds.getName().equals(sectionName)) {
                                        bds.setName(sectionName);
                                        daoSupport.saveEntity(bds);
                                    }
                                    sectionId = bds.getId();
                                } else {
                                    BookDocSection bds = new BookDocSection();
                                    bds.setName(sectionName);
                                    bds.setOrder(sectionNum);
                                    bds.setBookId(bd.getId());
                                    daoSupport.saveEntity(bds);
                                    sectionId = bds.getId();
                                }
                                NodeList pageList = ((Element) divNode).getElementsByTagName("LI");
                                for (int l = 0; l < pageList.getLength(); l++) {
                                    Node liNode = pageList.item(l);
                                    if (!liNode.hasAttributes() || !liNode.getAttributes().getNamedItem("class").getNodeValue().contains("dashedline")) {
                                        NodeList aList = ((Element) liNode).getElementsByTagName("A");
                                        if (aList.getLength() > 0) {
                                            pageNum++;
                                            if (pageNum > maxPartNum) {
                                                Node hrefNode = aList.item(0);
                                                String pageTitle = hrefNode.getTextContent().trim();
                                                String pageHref = hrefNode.getAttributes().getNamedItem("href").getNodeValue().trim();
                                                //更新页内容
                                                Document pageDoc = getDocument(HREF_FIX + pageHref);
                                                if (pageDoc == null) {
                                                    try {
                                                        Thread.sleep(5000);
                                                    } catch (InterruptedException e) {
                                                        e.printStackTrace();
                                                    }
                                                    pageDoc = getDocument(HREF_FIX + pageHref);
                                                    if (pageDoc == null) {
                                                        continue;
                                                    }
                                                }
                                                Element txtEmt = pageDoc.getElementById("contTxt");
                                                if (txtEmt != null) {
                                                    NodeList pList = txtEmt.getElementsByTagName("P");
                                                    StringBuffer contextBuffer = new StringBuffer();
                                                    for (int p = 0; p < pList.getLength(); p++) {
                                                        String pText = pList.item(p).getTextContent().trim();
                                                        if(pText.startsWith("未完待续")||pText.startsWith("新浪原创")){
                                                            break;
                                                        }
                                                        wordNum += pText.length();
                                                        contextBuffer.append("<p>").append(pText).append("</p>");
                                                    }
                                                    BookDocPart bdp = new BookDocPart();
                                                    bdp.setOrder(pageNum);
                                                    bdp.setTitle(pageTitle);
                                                    bdp.setContent(contextBuffer.toString());
                                                    bdp.setStoreType(ConstValueUtils.STORE_TYPE_DB);
                                                    bdp.setSectionId(sectionId);
                                                    bdp.setUpdateTime(new Date());
                                                    daoSupport.saveEntity(bdp);
                                                    seq = pageNum;
                                                } else {
                                                    break bookLabel;
                                                }
                                                try {
                                                    Thread.sleep(1000);
                                                } catch (InterruptedException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                bd.setWordNum(wordNum);
                daoSupport.saveEntity(bd);
                return seq - maxPartNum;
            }
        } catch (Exception ex) {
            return ConstValueUtils.CODE_SUCCESS;
        }
        return ConstValueUtils.CODE_SUCCESS;
    }

    public void update() {
        Document doc = getDocument(URL_INDEX.replace("#{page}", "1"));
        if (doc != null) {
            NodeList divList = doc.getElementsByTagName("DIV");
            BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
            JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
            String checkSql = " and name = ? and author = ? ";
            String checkTagSql = "select oid from book_document_tag where tag_name = ? ";
            Integer totalNum = 0;
            for (int i = 0; i < divList.getLength(); i++) {
                Node divNode = divList.item(i);
                if (divNode.hasAttributes()) {
                    NamedNodeMap attrMap = divNode.getAttributes();
                    Node clsAttrNode = attrMap.getNamedItem("class");
                    if (clsAttrNode != null) {
                        if (clsAttrNode.getNodeValue().contains("blk_num")) {
                            String totalStr = divNode.getChildNodes().item(1).getTextContent().trim();
                            if (!StringUtils.isTrimEmpty(totalStr)) {
                                totalNum = Integer.valueOf(totalStr);
                            }
                        } else if (clsAttrNode.getNodeValue().contains("blk_book")) {
                            NodeList iList = ((Element) divNode).getElementsByTagName("I");
                            if (iList.getLength() == 0) {
                                //VIP的不下载
                                NodeList aList = ((Element) divNode).getElementsByTagName("A");
                                Node hrefNode = aList.item(0);
                                String hrefStr = hrefNode.getAttributes().getNamedItem("href").getNodeValue();
                                String iconHref = ((Element) hrefNode).getElementsByTagName("IMG").item(0).getAttributes().getNamedItem("src").getNodeValue();
                                String title = aList.item(1).getTextContent().trim();
                                NodeList ulList = ((Element) divNode).getElementsByTagName("UL");
                                NodeList liList = ((Element) ulList.item(0)).getElementsByTagName("LI");
                                String author = liList.item(0).getTextContent().trim();//作者：朝天椒
                                String[] authorInfos = author.split("：");
                                if (authorInfos.length > 1) {
                                    author = authorInfos[1].trim();
                                }
                                //
                                List<BookDoc> bookList = daoSupport.listEntityByCondition(BookDoc.class, checkSql, new Object[]{title, author});
                                if (bookList.size() == 0) {
                                    String status = liList.item(1).getTextContent().trim();//状态：连载
                                    liList = ((Element) ulList.item(1)).getElementsByTagName("LI");
                                    String partition = liList.item(0).getTextContent().trim();//分类：军事小说
                                    String publisher = "";
                                    if (liList.getLength() > 1) {
                                        publisher = liList.item(1).getTextContent().trim();//出版：重庆出版社
                                        String[] pInfo = publisher.split("：");
                                        if (pInfo.length > 1) {
                                            publisher = pInfo[1].trim();
                                        }
                                    }
                                    NodeList pList = ((Element) divNode).getElementsByTagName("P");
                                    String brief = pList.item(0).getTextContent().trim();
                                    BookDoc bd = new BookDoc();
                                    bd.setName(title);
                                    bd.setAuthor(author);
                                    bd.setBookBrief(brief);
                                    bd.setPublisher(publisher);
                                    bd.setSourceType(ConstValueUtils.SOURCE_TYPE_SN);
                                    bd.setSourceId(hrefStr);
                                    bd.setWordNum(0);
                                    bd.setType(ConstValueUtils.BOOK_TYPE_COMMON);
                                    String[] statusInfo = status.split("：");
                                    if (statusInfo.length > 1) {
                                        status = statusInfo[1].trim();
                                    }
                                    bd.setStatus(StringUtils.isTrimEmpty(status) ? "" : (status.equals("连载") ? ConstValueUtils.STATUS_CONTINUE : ConstValueUtils.STATUS_CONTINUE));
                                    String[] pInfos = partition.split("：");
                                    bd.setPartition(pInfos.length > 1 ? pInfos[1].trim() : partition);
                                    //处理图标信息
                                    bd.setHasIcon(false);
                                    if (!StringUtils.isTrimEmpty(iconHref)) {
                                        byte[] imgData = downHttpData(iconHref);
                                        if (imgData != null && imgData.length > 0) {
                                            bd.setHasIcon(true);
                                            bd.setIcon(BlobProxy.generateProxy(imgData));
                                        }
                                    }
                                    bd.setUpdateTime(new Date());
                                    daoSupport.saveEntity(bd);
                                    NodeList keyAList = ((Element) pList.item(1)).getElementsByTagName("A");
                                    if (keyAList.getLength() > 0) {
                                        for (int t = 0; t < keyAList.getLength(); t++) {
                                            String keyStr = keyAList.item(t).getTextContent().trim();
                                            int tagId;
                                            List<Map<String, Object>> tagDbList = jt.queryForList(checkTagSql, new Object[]{keyStr});
                                            if (tagDbList.size() > 0) {
                                                tagId = ((Number) tagDbList.get(0).get("oid")).intValue();
                                            } else {
                                                BookDocTag bt = new BookDocTag();
                                                bt.setName(keyStr);
                                                daoSupport.saveEntity(bt);
                                                tagId = bt.getId();
                                            }
                                            BookDocTagRelation tr = new BookDocTagRelation();
                                            tr.setBookId(bd.getId());
                                            tr.setTagId(tagId);
                                            daoSupport.saveEntity(tr);
                                        }
                                    }
                                    //更新内容
                                    updateList(bd);
                                } else {
                                    if (bookList.get(0).getStatus().equals(ConstValueUtils.STATUS_CONTINUE)) {
                                        //未完待续的检查更新
                                        updateList(bookList.get(0));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (totalNum > PAGE_SIZE) {
                for (int page = 2; (page - 1) * PAGE_SIZE < totalNum; page++) {
                    doc = getDocument(URL_INDEX.replace("#{page}", String.valueOf(page)));
                    try {
                        if (doc != null) {
                            divList = doc.getElementsByTagName("DIV");
                            for (int i = 0; i < divList.getLength(); i++) {
                                Node divNode = divList.item(i);
                                NodeList iList = ((Element) divNode).getElementsByTagName("I");
                                if (iList.getLength() == 0) {
                                    if (divNode.hasAttributes()) {
                                        NamedNodeMap attrMap = divNode.getAttributes();
                                        Node clsAttrNode = attrMap.getNamedItem("class");
                                        if (clsAttrNode != null) {
                                            if (clsAttrNode.getNodeValue().contains("blk_book")) {
                                                NodeList aList = ((Element) divNode).getElementsByTagName("A");
                                                Node hrefNode = aList.item(0);
                                                String hrefStr = hrefNode.getAttributes().getNamedItem("href").getNodeValue();
                                                String iconHref = ((Element) hrefNode).getElementsByTagName("IMG").item(0).getAttributes().getNamedItem("src").getNodeValue();
                                                String title = aList.item(1).getTextContent().trim();
                                                NodeList ulList = ((Element) divNode).getElementsByTagName("UL");
                                                NodeList liList = ((Element) ulList.item(0)).getElementsByTagName("LI");
                                                String author = liList.item(0).getTextContent().trim();//作者：朝天椒
                                                String[] authorInfos = author.split("：");
                                                if (authorInfos.length > 1) {
                                                    author = authorInfos[1].trim();
                                                }
                                                //
                                                List<BookDoc> bookList = daoSupport.listEntityByCondition(BookDoc.class, checkSql, new Object[]{title, author});
                                                if (bookList.size() == 0) {
                                                    String status = liList.item(1).getTextContent().trim();//状态：连载
                                                    liList = ((Element) ulList.item(1)).getElementsByTagName("LI");
                                                    String partition = liList.item(0).getTextContent().trim();//分类：军事小说
                                                    String publisher = "";
                                                    if (liList.getLength() > 1) {
                                                        publisher = liList.item(1).getTextContent().trim();//出版：重庆出版社
                                                        String[] pInfo = publisher.split("：");
                                                        if (pInfo.length > 1) {
                                                            publisher = pInfo[1].trim();
                                                        }
                                                    }
                                                    NodeList pList = ((Element) divNode).getElementsByTagName("P");
                                                    String brief = pList.item(0).getTextContent().trim();
                                                    BookDoc bd = new BookDoc();
                                                    bd.setName(title);
                                                    bd.setAuthor(author);
                                                    bd.setBookBrief(brief);
                                                    bd.setPublisher(publisher);
                                                    bd.setSourceType(ConstValueUtils.SOURCE_TYPE_SN);
                                                    bd.setSourceId(hrefStr);
                                                    bd.setWordNum(0);
                                                    bd.setType(ConstValueUtils.BOOK_TYPE_COMMON);
                                                    String[] statusInfo = status.split("：");
                                                    if (statusInfo.length > 1) {
                                                        status = statusInfo[1].trim();
                                                    }
                                                    bd.setStatus(StringUtils.isTrimEmpty(status) ? "" : (status.equals("连载") ? ConstValueUtils.STATUS_CONTINUE : ConstValueUtils.STATUS_CONTINUE));
                                                    String[] pInfos = partition.split("：");
                                                    bd.setPartition(pInfos.length > 1 ? pInfos[1].trim() : partition);
                                                    //处理图标信息
                                                    bd.setHasIcon(false);
                                                    if (!StringUtils.isTrimEmpty(iconHref)) {
                                                        byte[] imgData = downHttpData(iconHref);
                                                        if (imgData != null && imgData.length > 0) {
                                                            bd.setHasIcon(true);
                                                            bd.setIcon(BlobProxy.generateProxy(imgData));
                                                        }
                                                    }
                                                    bd.setUpdateTime(new Date());
                                                    daoSupport.saveEntity(bd);
                                                    NodeList keyAList = ((Element) pList.item(1)).getElementsByTagName("A");
                                                    if (keyAList.getLength() > 0) {
                                                        for (int t = 0; t < keyAList.getLength(); t++) {
                                                            String keyStr = keyAList.item(t).getTextContent().trim();
                                                            int tagId;
                                                            List<Map<String, Object>> tagDbList = jt.queryForList(checkTagSql, new Object[]{keyStr});
                                                            if (tagDbList.size() > 0) {
                                                                tagId = ((Number) tagDbList.get(0).get("oid")).intValue();
                                                            } else {
                                                                BookDocTag bt = new BookDocTag();
                                                                bt.setName(keyStr);
                                                                daoSupport.saveEntity(bt);
                                                                tagId = bt.getId();
                                                            }
                                                            BookDocTagRelation tr = new BookDocTagRelation();
                                                            tr.setBookId(bd.getId());
                                                            tr.setTagId(tagId);
                                                            daoSupport.saveEntity(tr);
                                                        }
                                                    }
                                                    //更新内容
                                                    updateList(bd);
                                                } else {
                                                    if (bookList.get(0).getStatus().equals(ConstValueUtils.STATUS_CONTINUE)) {
                                                        //未完待续的检查更新
                                                        updateList(bookList.get(0));
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } catch (Exception ex) {
                        logger.error("更新第" + page + "页异常", ex);
                    }
                }
            }
        }
    }

    private void updateList(BookDoc bd) {
        int wordNum = bd.getWordNum();
        Document doc = getDocument(bd.getSourceId());
        if (doc != null) {
            NodeList divList = doc.getElementsByTagName("DIV");
            BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
            JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
            List<BookDocSection> sectionList = daoSupport.listEntityByCondition(BookDocSection.class, " and bookId = ? order by order asc", new Object[]{bd.getId()});
            String maxPartSql = "select IFNULL(max(part_order),0) mo from book_document_part where section_id in ( select oid from book_document_section where book_id = ? )";
            int maxPartNum = jt.queryForInt(maxPartSql, new Object[]{bd.getId()});
            int sectionNum = 0;
            int pageNum = 0;
            bookLabel:
            for (int d = 0; d < divList.getLength(); d++) {
                //chapter_list
                Node divNode = divList.item(d);
                if (divNode.hasAttributes()) {
                    NamedNodeMap attrMap = divNode.getAttributes();
                    Node clsAttrNode = attrMap.getNamedItem("class");
                    if (clsAttrNode != null) {
                        if (clsAttrNode.getNodeValue().contains("chapter_list")) {
                            //列表
                            sectionNum++;
                            String sectionName = "第" + sectionNum + "章";
                            NodeList hList = ((Element) divNode).getElementsByTagName("H3");
                            if (hList.getLength() > 0) {
                                sectionName = hList.item(0).getTextContent().trim();
                            }
                            int sectionId = 0;
                            if (sectionNum <= sectionList.size()) {
                                BookDocSection bds = sectionList.get(sectionNum - 1);
                                if (!bds.getName().equals(sectionName)) {
                                    bds.setName(sectionName);
                                    daoSupport.saveEntity(bds);
                                    sectionId = bds.getId();
                                }
                            } else {
                                BookDocSection bds = new BookDocSection();
                                bds.setName(sectionName);
                                bds.setOrder(sectionNum);
                                bds.setBookId(bd.getId());
                                daoSupport.saveEntity(bds);
                                sectionId = bds.getId();
                            }
                            NodeList pageList = ((Element) divNode).getElementsByTagName("LI");
                            for (int l = 0; l < pageList.getLength(); l++) {
                                Node liNode = pageList.item(l);
                                if (!liNode.hasAttributes() || !liNode.getAttributes().getNamedItem("class").getNodeValue().contains("dashedline")) {
                                    NodeList aList = ((Element) liNode).getElementsByTagName("A");
                                    if (aList.getLength() > 0) {
                                        pageNum++;
                                        if (pageNum > maxPartNum) {
                                            Node hrefNode = aList.item(0);
                                            String pageTitle = hrefNode.getTextContent().trim();
                                            String pageHref = hrefNode.getAttributes().getNamedItem("href").getNodeValue().trim();
                                            //更新页内容
                                            Document pageDoc = getDocument(HREF_FIX + pageHref);
                                            if (pageDoc == null) {
                                                try {
                                                    Thread.sleep(5000);
                                                } catch (InterruptedException e) {
                                                    e.printStackTrace();
                                                }
                                                pageDoc = getDocument(HREF_FIX + pageHref);
                                                if (pageDoc == null) {
                                                    continue;
                                                }
                                            }
                                            Element txtEmt = pageDoc.getElementById("contTxt");
                                            if (txtEmt != null) {
                                                NodeList pList = txtEmt.getElementsByTagName("P");
                                                StringBuffer contextBuffer = new StringBuffer();
                                                for (int p = 0; p < pList.getLength(); p++) {
                                                    String pText = pList.item(p).getTextContent().trim();
                                                    if (pText.length() <= 0) {
                                                        break;
                                                    }
                                                    wordNum += pText.length();
                                                    contextBuffer.append("<p>").append(pText).append("</p>");
                                                }
                                                BookDocPart bdp = new BookDocPart();
                                                bdp.setOrder(pageNum);
                                                bdp.setTitle(pageTitle);
                                                bdp.setContent(contextBuffer.toString());
                                                bdp.setStoreType(ConstValueUtils.STORE_TYPE_DB);
                                                bdp.setSectionId(sectionId);
                                                bdp.setUpdateTime(new Date());
                                                daoSupport.saveEntity(bdp);
                                            } else {
                                                break bookLabel;
                                            }
                                            try {
                                                Thread.sleep(1000);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            bd.setWordNum(wordNum);
            daoSupport.saveEntity(bd);
        }
    }

    private Document getDocument(String url) {
        try {
            HttpClient hc = new DefaultHttpClient();
            HttpGet hg = new HttpGet(url);
            hc.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
            hc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
            HttpResponse response = hc.execute(hg);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                DOMParser htc = new DOMParser();
                htc.setProperty("http://cyberneko.org/html/properties/default-encoding", CHARSET_HTML);
                htc.parse(new InputSource(response.getEntity().getContent()));
                hc.getConnectionManager().shutdown();
                return htc.getDocument();
            }
        } catch (Exception e) {
            logger.error("解析Html异常:" + url, e);
        }
        return null;
    }

    private byte[] downHttpData(String url) {
        try {
            HttpClient hc = new DefaultHttpClient();
            hc.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
            hc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
            HttpGet hg = new HttpGet(url);
            HttpResponse response = hc.execute(hg);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                response.getEntity().writeTo(bout);
                return bout.toByteArray();
            }
        } catch (Exception e) {
            logger.error("解析Html异常:" + url, e);
        }
        return null;
    }
}
